package asapIs134CK.womi.client.games.mathEnigme;


import asapIs134CK.womi.client.games.GameType;
import asapIs134CK.womi.client.games.GameWidget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ArithmeticSequence extends GameWidget{
	private VerticalPanel panel;	
	private FlowPanel toolbar;
	private Button newGame;
	private ClickHandler newGameClickHandler;
	private ListBox difficultyListBox;
	private ArithmeticQuizz currentQuizz;
	public static final int VERYEASY = 0;
	public static final int EASY = 1;
	public static final int NORMAL = 2;
	public static final int HARD = 3;
	public static final int VERYHARD = 4;
	
	private int quizzesLeft;
	private int difficulty;
	private int score;
	private DialogBox dbEndOfGame;
	private FlowPanel results;
	private Label lblScore;
	private Label pagination;
	
	public ArithmeticSequence(){
		super();
		panel = new VerticalPanel();
		panel.setStyleName("ArithmeticSequence");
		toolbar = new FlowPanel();
		toolbar.setStyleName("Toolbar");
		newGameClickHandler = new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				handleNewgame();
			}
			
		};
		newGame = new Button("<img src=\"imgs/PNG/newGame.png\" alt=\"\" /> New game", newGameClickHandler);
		toolbar.add(newGame);
		difficultyListBox = new ListBox();
		difficultyListBox.addItem("Very easy", VERYEASY+"");
		difficultyListBox.addItem("Easy", EASY+"");
		difficultyListBox.addItem("Normal", NORMAL+"");
		difficultyListBox.addItem("Hard", HARD+"");
		difficultyListBox.addItem("Very hard", VERYHARD+"");
		toolbar.add(difficultyListBox);
		panel.add(toolbar);
		score = 0;
		results = new FlowPanel();
		results.setStyleName("Results");
		lblScore = new Label("Score : "+score+" ");
		pagination = new Label(" Game : 1/10");
		results.add(lblScore);
		results.add(pagination);
		panel.add(results);
		difficulty = VERYEASY;
		quizzesLeft = 9;
		
		currentQuizz = new ArithmeticQuizz(difficulty, this);
		panel.add(currentQuizz);
		/*Button forDebug =  new Button("forDebug", new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				currentQuizz.setFocus(true);
			}
		});
		panel.add(forDebug);*/
		initWidget(panel);
	}
	


	private void handleNewgame() {
		quizzesLeft = 9;
		score = 0;
		lblScore.setText("Score : "+score+" ");
		pagination.setText(" Game : 1/10");
		panel.remove(currentQuizz);
		difficulty = Integer.parseInt(difficultyListBox.getValue(difficultyListBox.getSelectedIndex()));
		currentQuizz = new ArithmeticQuizz(difficulty, this);
		panel.add(currentQuizz);
		currentQuizz.setFocus(true);
	}
	
	public void nextGame(boolean correct){
		if(correct){
			score+=difficulty+1;
			lblScore.setText("Score : "+score+" ");
		}
		if(quizzesLeft > 0){
			panel.remove(currentQuizz);
			currentQuizz = new ArithmeticQuizz(difficulty, this);
			panel.add(currentQuizz);
			currentQuizz.setFocus(true);
			quizzesLeft--;
			pagination.setText("game : "+(10-quizzesLeft)+"/10");
		}
		else {
			handleEndOfGame();
		}
	}
	private void handleEndOfGame(){
		panel.remove(currentQuizz);
		submitScore(score, GameType.Arithmetic);
		dbEndOfGame = new DialogBox();
		dbEndOfGame.center();
		dbEndOfGame.add(new Label("You scored "+score+" out of "+(10*(difficulty+1))+" points !"));
		dbEndOfGame.setAutoHideEnabled(true);
		dbEndOfGame.show();
	}



	public void setFocus(boolean b) {
		//System.out.println("ArithSeq:setFocus -> propage");
		currentQuizz.setFocus(b);
	}



	
}
